program rad2brt

    use iasing_osse, only:  &
        JPI4,               &
        JPR4,               &
        JPR8,               &
        NIASI,              &
        NIANG,              &
        r4a_ids,            &
        r60_dat
        
    use netcdf

    implicit none
    !
    integer             :: ja
    integer             :: open_mode
    ! 
    character(len=128)  :: dbfile
    character(len=3)    :: units
    integer             :: dbid
    type(r4a_ids)       :: db_ids
    type(r60_dat)       :: db_dat
    ! 
    logical             :: lex, lerr
    logical             :: brt_units = .false.
    integer(kind=JPI4)  :: natm
    
    real(JPR4), allocatable, dimension(:,:) :: ria, rng, ngb
    real(JPR4)          :: err_ria(NIASI), err_rng(NIANG)

    include "checknc.interface"
    include "rad2brt_f.interface"
    include "brt2rad_f.interface"
    include "r60_initializer.interface"

    ! Initialization of error vars
    lerr = .false.
    err_ria = -999.9
    err_rng = -999.9
    open_mode = NF90_NOWRITE
    open_mode = NF90_WRITE

    ! Get argument 
    call get_command_argument(1, dbfile)
    if (len_trim(dbfile)==0) then
        print*, "No file provided. Aborting..."
        stop -1
    else
        inquire(file=dbfile, exist=lex)
        if (.not.lex) then
            write(*,'(3A)') "File ", trim(dbfile), " does not exits. STOP"
            stop -2
        endif
    endif
    print*, "dbfile: ", trim(dbfile)

    ! open and initialize databank 
    print*, "previous openning file"
    call checknc(nf90_open(trim(dbfile), open_mode, dbid))
    db_ids = r60_initializer(dbid)
    print*, "File opened and initialized..."

    call checknc(nf90_inquire_dimension(dbid, db_ids%atm_did, len=natm))
    print*, "Number of atmospheres ", natm

	! Allocation of vars
    allocate(ria(NIASI, natm))
    allocate(rng(NIANG, natm))
    allocate(ngb(NIANG, natm))
    print*, "Allocation of 'ria', 'rng' and 'ngb' "

    ! Load vars
    call checknc(nf90_get_var(db_ids%rbt%gid, db_ids%rbt%ria, ria))
    call checknc(nf90_get_var(db_ids%rbt%gid, db_ids%rbt%rng, rng))
    call checknc(nf90_get_var(db_ids%rbt%gid, db_ids%rbt%ngb, ngb))
    

    do ja = 1, natm
        lerr = .false.
        !if (ja < 4353 .or. ja >4416) cycle
        !if (ria(0,ja)>400)print*, ja, ria(1,ja), huge(ria(1,1))
        if (ria(1,ja) < 100. .or. ria(1,ja)>400.) then
            print*, "Error in ria, simulation ", ja
            lerr=.true.
            if (open_mode == NF90_WRITE ) call checknc(nf90_put_var(    &
                db_ids%rbt%gid, db_ids%rbt%ria, err_ria, &
                start=(/1,ja/), count=(/NIASI,1/) ))
        endif
        if (rng(1,ja) < 100. .or. rng(1,ja)>400.) then
            print*, "Error in rng, simulation ", ja
            lerr=.true.
            if (open_mode == NF90_WRITE ) call checknc(nf90_put_var(    &
                db_ids%rbt%gid, db_ids%rbt%rng, err_rng, &
                start=(/1,ja/), count=(/NIASI,1/) ))
        endif
        !if (ngb(1,ja) < 100. .or. ngb(1,ja)>400.) then
        !    print*, "Error in ngb, simulation ", ja
        !    lerr=.true.
        !    if (open_mode == NF90_WRITE ) call checknc(nf90_put_var(    &
        !        db_ids%rbt%gid, db_ids%rbt%ngb, err_rng, &
        !        start=(/1,ja/), count=(/NIASI,1/) ))
        !endif
        if (lerr) print*
    enddo

    ! Deallocation
    deallocate(ria)
    deallocate(rng)
    deallocate(ngb)

    ! Close file
    call checknc(nf90_close(dbid))

end program
